<?php
namespace app\api\command;

use app\api\controller\Base;
use app\common\model\LoginLog;
use app\common\model\Users;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class UserActive extends Command
{

    /**
     * 用户活跃度统计
     */
    protected function configure()
    {
        $this->setName('user_active')->setDescription('user active count');
    }

    protected function execute(Input $input, Output $output)
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);
        $cache = cache('command'.__CLASS__);
        if ($cache){
            $output->info('running');
            return false;
        }else{
            cache('command'.__CLASS__,true,1800);
        }

        $model = new \app\common\model\UserActive();

        $last  = $model->where('type',1)->order('id desc')->find();

        if ($last){
            $start_time = $last['date'];
            $last_date = $last['date'];
        }else{
            $start_time = Carbon::now()->startOfDay()->timestamp;
            $last_date  = Carbon::now()->subDay()->startOfDay()->toDateTimeString();
        }

        //小时统计
        $hour['create_time'] = ['between time',[$start_time,Carbon::now()->timestamp]];

        //活跃男用户
        $hour['sex'] = 1;
        $item['man'] = LoginLog::where($hour)
            ->group('user_id')
            ->count();

        //活跃女用户
        $hour['sex'] = 2;
        $item['woman'] = LoginLog::where($hour)
            ->group('user_id')
            ->count();

        //新增男用户
        $new['create_time'] =  ['between time',[$start_time,Carbon::now()->timestamp]];
        $new['sex']         = 1;
        $item['new_man'] = Db::name('users')->where($new)->count();

        //新增女用户
        $new['sex'] = 2;
        $item['new_woman'] = Db::name('users')->where($new)->count();

        $item['date'] = Carbon::now()->toDateTimeString();

        $item['type'] = 1;
        $item['last_date'] = $last_date;
        $item['user_total'] =  Users::count();
        $item['create_time'] = time();
        $item['update_time'] = time();

        $model->insert($item);

        //查询是否有昨日活跃总统计
        $yesterday = $model->where('type',2)->whereTime('date','yesterday')->find();

        if (!$yesterday){
            $item = [];
            //昨日活跃未统计  统计昨日活跃
            $item['man'] = LoginLog::whereTime('create_time','yesterday')
                ->where('sex',1)
                ->group('user_id')
                ->count();
            $item['woman'] = LoginLog::whereTime('create_time','yesterday')
                ->where('sex',2)
                ->group('user_id')
                ->count();

            $user_total = Users::whereTime('create_time', '<', Carbon::now()->startOfDay()->toDateTimeString())
                ->count();

            //新增男用户
            $item['new_man'] = Db::name('users')->where('sex',1)
                ->whereTime('create_time','yesterday')
                ->count();

            //新增女用户
            $item['new_woman'] = Db::name('users')
                ->where('sex',2)
                ->whereTime('create_time','yesterday')
                ->count();

            $item['create_time'] = time();
            $item['update_time'] = time();
            $item['date']   = Carbon::now()->subDay(1)->endOfDay()->toDateTimeString();

            $item['type']   = 2;
            $item['last_date']  = Carbon::now()->subDay(2)->endOfDay()->toDateTimeString();
            $item['user_total'] = $user_total;
            $model->insert($item);

        }

        cache('command'.__CLASS__,null);
    }


}